# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=xalan-c
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.12
pkgrel=14
pkgdesc="An XSLT processing library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://xalan.apache.org/xalan-c"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-xerces-c")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-tools")
source=(https://archive.apache.org/dist/xalan/xalan-c/sources/xalan_c-${pkgver}.tar.gz
        01-mingw-icu.patch
        02-mingw-as-msvc.patch
        03-install-layout.patch
        04-no-std-file.patch
        05-set-cxx-std-17.patch
        06-fix-build-with-clang-19.patch)
sha256sums=('ee7d4b0b08c5676f5e586c7154d94a5b32b299ac3cbb946e24c4375a25552da7'
            '64ca2fb25114fd9c35c0a0815c7308ab3222eb421965892a402c4f30d23f180b'
            '8ccef1339fcfb6472861545295a9cb72a6b8da3a81407d082a5df51ac5f5c8e0'
            '35f2cf6b4aefb29c1c88f94b808c07a2b2845c025f236b38f4e31b5e378ba874'
            '0b2bcce90c90261e309a7d3914631f91deaca00930acda487c9b828c581ea968'
            '1af451cf368fc50e48ffc6f17f987f4e51e9facd1e811ca477026d88eedee9bb'
            '507de5146717d647775af15ae1f59d4584b5060ea038da4531a42cd124750a8d')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare(){
  cd "${srcdir}"/${_realname//-/_}-${pkgver}
  apply_patch_with_msg \
    01-mingw-icu.patch \
    02-mingw-as-msvc.patch \
    03-install-layout.patch \
    04-no-std-file.patch \
    05-set-cxx-std-17.patch \
    06-fix-build-with-clang-19.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_CXX_STANDARD=17 \
      "${extra_config}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      ../${_realname//-/_}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}"/${_realname//-/_}-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
